package com.cos.shop.web.controller;

import cn.dsna.util.images.ValidateCode;
import com.cos.shop.common.Const;
import com.cos.shop.dao.UserDao;
import com.cos.shop.entity.User;
import com.cos.shop.services.UserServices;
import com.cos.shop.services.impl.UserServicesImpl;
import com.cos.shop.util.Base64Utils;
import com.cos.shop.util.StringUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/2/23 11:36
 * @description:
 */
@WebServlet("/user")
public class UserController extends BaseServlet {
    private UserServices us = new UserServicesImpl();
    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/23 17:33
     * @description: 注销
     */
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //注销session对象
        request.getSession().invalidate();
        //删除cookie保留的自动登录标识
        //覆盖
        Cookie c = new Cookie(Const.AUTO_LOGIN,"");
        //存活时间设置为0
        c.setMaxAge(0);
        //作用域和保存自动登录标识的作用域要一样
        c.setPath("/");
        //写出cookie 就删除了cookie
        response.addCookie(c);
        //转index/login
        return Const.REDIRECT+"index.jsp";
    }


    /**
     * @author: COS
     * @return:
     * @time: 2022/2/23 16:32
     * @description: 登录验证
     */
    public String login(HttpServletRequest request, HttpServletResponse response) {
        //1 获取用户输入的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2 访问业务层操作
        User u = us.login(username, password);
        //3 判断结果
        if (u != null) {
            if (u.getUstatus()== Const.NOT_ACTIVE) {
                //登录失败 庄户未激活
                request.setAttribute("msg", "账户未激活,先请激活下您的脑子");
                return Const.FORWARD+"login.jsp";
            }else{
                //保存数据到session
                request.getSession().setAttribute(Const.IS_LOGIN,u);
                //用户是否设置自动登录
                String auto = request.getParameter("auto");
                if (StringUtils.isNotEmpty(auto)&&auto.equals("on")) {
                    //用户设置了自动登录 请求登录页面login.jsp 检查 有设置自动登录 帮用户做登录
                    //保存一个标识号 cookie 为了数据安全把用户名和密码进行编码,并保存到cookie
                    //方便辨认处理用户的自动登录
                    Cookie c = new Cookie(Const.AUTO_LOGIN, Base64Utils.encode(username+":"+password));
                    //存活时间
                    c.setMaxAge(60*60*24*14);
                    //作用域
                    c.setPath("/");
                    //把cookie写出到客户端
                    response.addCookie(c);
                }
                return Const.REDIRECT+"index.jsp";
            }
        }
        //登录失败
        request.setAttribute("msg", "登录失败");
        return Const.FORWARD+"login.jsp";
    }


    /**
     * @author: COS
     * @return:
     * @time: 2022/2/23 16:28
     * @description: 验证码验证
     */
    public String checkCode(HttpServletRequest request, HttpServletResponse response) {
        String code = request.getParameter("code");
        String valiCode = (String) request.getSession().getAttribute(Const.VALI_DATA_CODE);
        //验证
        if (valiCode.equalsIgnoreCase(code)) {
            return Const.SUCCESS;
        }
        return Const.FAIL;
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/2/23 16:20
     * @description:获取验证码 图片是直接通过工具类写出, 不需要父类做处理, 直接void
     */
    public void code(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1 创建激活码对象
        ValidateCode vc = new ValidateCode(120, 30, 4, 30);
        //2 获取激活码保存到session
        String code = vc.getCode();
        System.out.println(code);
        request.getSession().setAttribute(Const.VALI_DATA_CODE, code);
        //3 写出图片
        vc.write(response.getOutputStream());
    }

    /**
     * @author: COS
     * @return: 邮箱激活
     * @time: 2022/2/23 16:01
     * @description:
     */
    public String activate(HttpServletRequest request, HttpServletResponse response) {
        //获取用户参数
        String c = request.getParameter("c");
        String e = request.getParameter("e");
        //访问业务层处理业务
        System.out.println(1);
        String msg = us.active(e, c);
        //保存提示到request
        request.setAttribute("msg", msg);
        //转发
        System.out.println(2);
        return Const.FORWARD + "message.jsp";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/2/23 16:01
     * @description: 注册
     */
    public String reg(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
//        String uname = request.getParameter("uname");
//        String upassword = request.getParameter("upassword");
//        String usex = request.getParameter("usex");
//        //....
        //使用map集合获取所有的request中的参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        User u = new User();
        //使用工具封装数据
        BeanUtils.populate(u, parameterMap);
        //调用业务层
        boolean ans = us.reg(u);
        //判断业务处理结果
        if (ans) {
            //成功
            return Const.REDIRECT + "registerSuccess.jsp";
        }
        //失败
        request.setAttribute("msg", "注册失败");
        return Const.FORWARD + "message.jsp";
    }


    /**
     * @author: COS
     * @return:
     * @time: 2022/2/23 14:34
     * @description: 检查用户名是否存在
     */
    public String checkUserName(HttpServletRequest request, HttpServletResponse response) {
        //1 接受用户提交的参数
        String username = request.getParameter("username");
        //2 请求业务层业务操作
        User user = us.checkUserName(username);
        //3 判断对象是否存在,给与不同的响应
        if (user != null) {
            //返回代表存在对象的1
            return Const.SUCCESS;
        }
        //返回代表不存在对象的0
        return Const.FAIL;
    }

}
